package table.models;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class RankingPrintTableModel extends AbstractTableModel {

	private ArrayList < String > columnNames;
	private ArrayList < ArrayList < Object > > rowData;

	public RankingPrintTableModel() {
		this.rowData = new ArrayList < ArrayList < Object > >();
		this.columnNames = new ArrayList < String >();

	}

	public void setColumnNames(ArrayList < String > columnNames) {
		this.columnNames = columnNames;
	}

	public ArrayList < String > getColumnNames() {
		return this.columnNames;
	}

	public ArrayList < Object > getColumnNamesAsObjects() {
		ArrayList < Object > toReturn = new ArrayList < Object >();
		for (int i = 0; i < this.columnNames.size(); i++) {
			toReturn.add(this.columnNames.get(i));
		}

		return toReturn;
	}

	public void removeColumn(int columnIndex) {
		this.columnNames.remove(columnIndex);
		for (ArrayList < Object > singleRow : this.rowData) {
			singleRow.remove(columnIndex);
		}
	}

	public void insertColumn(int columnIndex, String columnName) {
		this.columnNames.add(columnIndex, columnName);
		for (ArrayList < Object > singleRow : this.rowData) {
			singleRow.add(columnIndex, new String(""));
		}
	}

	@Override
	public int getColumnCount() {
		return this.columnNames.size();
	}

	@Override
	public String getColumnName(int column) {
		return this.columnNames.get(column);
	}

	@Override
	public boolean isCellEditable(int row, int col) {
		return false;
	}

	@Override
	public int getRowCount() {
		return this.rowData.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return this.rowData.get(rowIndex).get(columnIndex);
	}

	@Override
	public void setValueAt(Object value, int row, int col) {
		while (this.getRowCount() < row) {
			this.addRow();
		}
		ArrayList < Object > tmp = this.rowData.get(row);
		tmp.set(col, value.toString());
		this.fireTableCellUpdated(row, col);
	}

	public void addRow() {
		ArrayList < Object > toAdd = new ArrayList < Object >();
		while (toAdd.size() < this.columnNames.size()) {
			toAdd.add(new String(""));
		}
		this.rowData.add(toAdd);

		this.fireTableRowsInserted(this.rowData.size() - 1, this.rowData.size() - 1);
	}

	public void addRow(ArrayList < Object > toAdd) {
		while (toAdd.size() < this.columnNames.size()) {
			toAdd.add(new String(""));
		}
		this.rowData.add(toAdd);

		this.fireTableRowsInserted(this.rowData.size() - 1, this.rowData.size() - 1);
	}

	@Override
	public Class getColumnClass(int c) {
		return this.getValueAt(0, c).getClass();
	}

	public ArrayList < Object > getRowData(int row) {
		return this.rowData.get(row);
	}

	public void removeRow(int row) {
		this.rowData.remove(row);
		this.fireTableDataChanged();
	}

	public void removeData() {
		this.rowData = new ArrayList < ArrayList < Object > >();
	}

}
